%% ex3.m
% set dir: src/
close all;
clear; clc;

% mandril_color
imgM = imread("../data/mandril_color.tif");
imgM_gray_avg = rgb1gray(imgM,'average');
imgM_gray_ntsc = rgb1gray(imgM,'NTSC');
figure('Position',[100,100,900,300])
subplot(1,3,1)
imshow(imgM)
axis off
title('Origin')
subplot(1,3,2)
imshow(imgM_gray_avg,[0,255])
axis off
title('Grayscale converted by Average')
subplot(1,3,3)
imshow(imgM_gray_ntsc,[0,255])
title('Grayscale converted by NTSC')
axis off
saveas(gcf,"../assets/mandril_to_gray.png")

% lena_color
imgL = imread("../data/lena512color.tiff");
imgL_gray_avg = rgb1gray(imgL,'average');
imgL_gray_ntsc = rgb1gray(imgL,'NTSC');
figure('Position',[100,100,900,300])
subplot(1,3,1)
imshow(imgL)
axis off
title('Origin')
subplot(1,3,2)
imshow(imgL_gray_avg,[0,255])
title('Grayscale converted by Average')
axis off
subplot(1,3,3)
imshow(imgL_gray_ntsc)
title('Grayscale converted by NTSC')
axis off
saveas(gcf,"../assets/lena_to_gray.png")
%% rgb1gray
function g = rgb1gray(f, method)
% Transform RGB images into grayscale images using 2 optional methods.
% f(matrix): RGB image matrix.
% method(string): choose from 'average' and 'NTSC', default 'NTSC'.
if ~exist('method')
    method = 'NTSC';
end

if strcmp(method,'NTSC')
    g = f(:,:,1) * 0.2989 + f(:,:,2) * 0.5870 + f(:,:,3) * 0.1140;
elseif strcmp(method,'average')
    g = mean(f,3);
else
    error("ERROR IN PARAMS, PLEASE CHECK PARA")
end
end
